﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.IO;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace ImageUploader
{
    class tcpclient
    {
        private static TcpClient client;//TcpClient，暂时不用了
        //tcpclient的方式传输文件，比较简单，不如直接用socket灵活，先留着备用吧
        private static void Send_File(string path)
        {
            Thread.Sleep(300);
            try
            {
                string ip = ConfigurationSettings.AppSettings["Server"].ToString();
                int port = Convert.ToInt32(ConfigurationSettings.AppSettings["Port"].ToString());
                client = new TcpClient();
                client.Connect(IPAddress.Parse(ip), port);
                NetworkStream ns = client.GetStream();
                FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
                int size = 0;
                long len = 0;
                //htCode["fileSize"] = fs.Length.ToString();
                while (len < fs.Length)
                {
                    byte[] buffer = new byte[512];
                    size = fs.Read(buffer, 0, buffer.Length);
                    ns.Write(buffer, 0, size);
                    len += size;
                }
                fs.Flush();
                ns.Flush();
                fs.Close();
                ns.Close();
                //ShowMsg.showMsg(listB, path + "文件发送成功");
            }
            catch (Exception ex)
            {
                //ShowMsg.showMsg(listB, ex.Message);
            }
        }

    }
}
